package io.zcoder.innoway.activities;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import io.zcoder.innoway.R;
import io.zcoder.innoway.commons.PrefUtils;
import io.zcoder.innoway.service.ReminderBroadcast;

public class ReminderActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {

    @BindViews({R.id.ic_sun, R.id.text_hour_sun, R.id.reminder_info_1, R.id.morning})
    View[] mSunViews;
    @BindViews({R.id.ic_night, R.id.text_hour_night, R.id.reminder_info_2, R.id.night})
    View[] mNightViews;
    @BindView(R.id.switch_reminder_1)
    Switch mSwitch1;
    @BindView(R.id.switch_reminder_2)
    Switch mSwitch2;

    private PendingIntent mPendingIntent;
    private AlarmManager mAlarmManager;
    private long mTime1;
    private long mTime2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_reminder_activity);
        ButterKnife.bind(this);
        showBack(true);
        setTitle(getString(R.string.reminder));

        Intent myIntent = new Intent(this, ReminderBroadcast.class);
        mPendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);
        mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 9);
        calendar.set(Calendar.MINUTE, 0);
        mTime1 = calendar.getTimeInMillis();
        calendar.set(Calendar.HOUR_OF_DAY, 21);
        calendar.set(Calendar.MINUTE, 30);
        mTime2 = calendar.getTimeInMillis();

        mSwitch1.setOnCheckedChangeListener(this);
        mSwitch2.setOnCheckedChangeListener(this);

        if (PrefUtils.getInt(this, PrefUtils.PREF_REMINDER_1) == 0) {
            mSwitch1.setChecked(false);
        }
        if (PrefUtils.getInt(this, PrefUtils.PREF_REMINDER_2) == 0) {
            mSwitch2.setChecked(false);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        float alpha = isChecked ? 1 : 0.3f;
        if (buttonView.getId() == R.id.switch_reminder_1) {
            PrefUtils.setInt(this, PrefUtils.PREF_REMINDER_1, isChecked ? 1 : 0);
            setService(isChecked, mTime1);
            for (View view : mSunViews) {
                view.setAlpha(alpha);
            }
        } else if (buttonView.getId() == R.id.switch_reminder_2) {
            PrefUtils.setInt(this, PrefUtils.PREF_REMINDER_2, isChecked ? 1 : 0);
            setService(isChecked, mTime2);
            for (View view : mNightViews) {
                view.setAlpha(alpha);
            }
        }
    }

    void setService(boolean checked, long time) {
        if (checked) {
            mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time,
                    AlarmManager.INTERVAL_DAY, mPendingIntent);
        } else {
            mAlarmManager.cancel(mPendingIntent);
        }

    }
}
